// JavaScript Document

function turnOn(){
	var AudioOn = document.getElementById('turnOn');
	//alert(Audio.className);
	if(AudioOn.className == 'On'){
		AudioOn.play();
		AudioOn.className = 'Off';
	}
}

function playPause(player) {
	//var Audio = document.getElementsByTagName('audio')[player];
	var flame = document.getElementById('candle-light-content');
	var FirstPlayer = document.getElementById('first-player');
	var SecondPlayer = document.getElementById('second-player');
	var ThirdPlayer = document.getElementById('third-player');
		
	var AudioOn = document.getElementById('turnOn');
		
	//Audio.play();
	//flame.style.display = "inline";
	
	if(player=='0'){
		var Audio = document.getElementById('audio1_es');
		Audio.play();
		setTimeout(function() {
			$('#candle-light-content').fadeOut(2000);
			AudioOn.className = 'On';
			//flame.style.display = 'none';
			flame.className = 'move';
			FirstPlayer.className = 'next-player';
			SecondPlayer.className = 'actual-player';
			smoke();
		},19000);
	}
		
	if(player=='1'){
		var Audio = document.getElementById('audio1_en');
		Audio.play();
		setTimeout(function() {
			$('#candle-light-content').fadeOut(2000);
			AudioOn.className = 'On';
			//flame.style.display = 'none';
			flame.className = 'move';
			FirstPlayer.className = 'next-player';
			SecondPlayer.className = 'actual-player';
			smoke();
		},21000);
	}
		
	if(player=='2'){
		var Audio = document.getElementById('audio2_es');
		Audio.play();
		setTimeout(function() {
			$('#candle-light-content').fadeOut(2000);
			AudioOn.className = 'On';
			//flame.style.display = 'none';
			flame.className = 'move';
			SecondPlayer.className = 'next-player';
			ThirdPlayer.className = 'actual-player';
			smoke();
		},24000);
	}
		
	if(player=='3'){
		var Audio = document.getElementById('audio2_en');
		Audio.play();
		setTimeout(function() {
			$('#candle-light-content').fadeOut(2000);
			AudioOn.className = 'On';
			//flame.style.display = 'none';
			flame.className = 'move';
			SecondPlayer.className = 'next-player';
			ThirdPlayer.className = 'actual-player';
			smoke();
		},24000);
	}
		
	if(player=='4'){
		var Audio = document.getElementById('audio3_es');
		Audio.play();
		setTimeout(function() {
			$('#candle-light-content').fadeOut(2000);
			AudioOn.className = 'On';
			//flame.style.display = 'none';
			flame.className = 'move';
			ThirdPlayer.className = 'next-player';
			FirstPlayer.className = 'actual-player';
			smoke();
		},31000);
	}
		
	if(player=='5'){
		var Audio = document.getElementById('audio3_en');
		Audio.play();
		setTimeout(function() {
			$('#candle-light-content').fadeOut(2000);
			AudioOn.className = 'On';
			//flame.style.display = 'none';
			flame.className = 'move';
			ThirdPlayer.className = 'next-player';
			FirstPlayer.className = 'actual-player';
			smoke();
		},31000);
	}
	
	document.getElementById('text-content').innerHTML = '';
}

function smoke(){
	var smokeImage = document.getElementById('smoke-image');

	setTimeout(function(){
		$('#smoke-image').attr('src','images/smoke.gif');
		$('#smoke-content').fadeIn(500);
		setTimeout(function(){
			$('#smoke-content').fadeOut(700);		
			setTimeout(function(){
				$('#smoke-image').attr('src','');
			},700);
		},5000);
	},300);	
}

function checkOrient(){
	if(typeof window.onorientationchange != 'undefined'){
		if ( orientation == 0 || orientation == 180 ) {
			/*** portail position **/
			/* smoke position and dimention */
			smoke.style.left = "478px";
			smoke.style.top = "21px";
			smoke_image.style.height = "306px";
			smoke_image.style.width = "102px";
		}
		else if ( orientation == 90 || orientation == -90 ) {
			/**landscape position **/
			/* smoke position and dimention */
			smoke.style.left = "802px";
			smoke.style.top = "0px";
			smoke_image.style.height = "240px";
			smoke_image.style.width = "80px";
		}
	}	
}

function which_player(lang){
	
	var FirstPlayer = document.getElementById('first-player');
	var SecondPlayer = document.getElementById('second-player');
	var ThirdPlayer = document.getElementById('third-player');
	
	
	if(ThirdPlayer.className == 'actual-player'){
		if(lang=="en"){
	
			subVideo3_en();
			playPause(5);
			return;
		} else {
	
			subVideo3_es();
			playPause(4);
			return;
		}
		
		
	}
	if(SecondPlayer.className == 'actual-player'){
		if(lang=="en"){
	
			subVideo2_en();
			playPause(3);
			return;
		} else {
	
			subVideo2_es();
			playPause(2);
			return;
		}
		
	}
	if (FirstPlayer.className == 'actual-player'){
		if(lang=="en"){
	
			subVideo1_en();
			playPause(1);
			return;
		} else {
	
			subVideo1_es();
			playPause(0);
			return;
		}
	}
}
	
    $(function() {
       	var logdiv = $("#log");
		var pageXStartPos;
		var pageYStartPos;
		var pageXactualPos;
		var pageYactualPos;
		var diff;
		var diffY;
		
		
		var AudioOn = document.getElementById('turnOn');
		var text_content = document.getElementById('text-content');
		var flame = document.getElementById('candle-light-content');
		var smoke = document.getElementById('smoke-content');
		var smoke_image = document.getElementById('smoke-image');
		var candleStick = document.getElementById('candle-stick-content');
		
		
		detectOrientation();
		window.onorientationchange = detectOrientation;
		
		function detectOrientation(){
			if(typeof window.onorientationchange != 'undefined'){
				if ( orientation == 0 || orientation == 180 ) {
					/*** portail position **/
					if(flame.className == 'not-move'){
					  flame.style.left = "440px";
					  flame.style.top = "100px";
					}
				}
				else if ( orientation == 90 || orientation == -90 ) {
					if(flame.className == 'not-move'){
						flame.style.left = "750px";
						flame.style.top = "30px";
					}
				}
			}
		}
		
		/*
		window.addEventListener("orientationchange", function() {
		  if(window.orientation==0||window.orientation==180){
			  candleStick.style.width = "768px";
			  candleStick.style.height = "1024px";
			  candleStick.style.backgroundSize = "758px 1024px";
			  candleStick.style.backgroundImage = "url(images/candle.png)";
		  }else{
			  candleStick.style.width = "1024px";
			  candleStick.style.height = "768px";
			  candleStick.style.backgroundSize = "1014px 758px";
			  candleStick.style.backgroundImage = "url(images/candle-h.png)";
		  }
		  
		}, false);
		*/
        document.addEventListener('touchstart', function(e) {
			pageXStartPos = e.changedTouches[0].pageX;
			pageYStartPos = e.changedTouches[0].pageY;
        }, false);
		
		document.addEventListener('touchmove', function(e) {
			e.preventDefault();
			pageXactualPos = e.targetTouches[0].pageX;
            pageYactualPos = e.targetTouches[0].pageY;
            
			diff = pageXStartPos-pageXactualPos;
			diffY = pageYStartPos-pageYactualPos;
			
			/* printpage position touch */
			//logdiv.html('X: '+pageXactualPos+'; Y: '+pageYactualPos+' - Diff: '+diff+' - DiffY: '+diffY);
			
			/************make flame move ***************/
			if(flame.style.display == 'inline'){
					if(flame.className == 'move'){
						if(parseInt(pageXactualPos)<160){
							pageXactualPos=160;
						}
						if(parseInt(pageYactualPos)<320){
							pageYactualPos=320;
						}
						flame.style.left = (pageXactualPos-160)+'px';
						flame.style.top = (pageYactualPos-320)+'px';
					}
				} else {
					if(flame.className == 'moving'){
						if(parseInt(pageXactualPos)<160){
							pageXactualPos=160;
						}
						if(parseInt(pageYactualPos)<320){
							pageYactualPos=320;
						}
						flame.style.left = (pageXactualPos-160)+'px';
						flame.style.top = (pageYactualPos-320)+'px';
					} else {
						/************ light on flame *****************/
						if(diff<-200||diff>200||diffY<-200||diffY>200){
							if(flame.className == 'move'){
								/****** put the start position to -200 so the flame can move all the screen *****/
								pageXStartPos=-200;
								pageYStartPos=-200;
								//flame.style.display = 'inline';
								turnOn();
								//AudioOn.play();
								$('#candle-light-content').fadeIn(1000);
								flame.style.left = (pageXactualPos-160)+'px';
								flame.style.top = (pageYactualPos-320)+'px';
							}
						}
					}
				}
				
			
			if(window.innerHeight > window.innerWidth){
			// code for Portrait orientation   
				if (((pageXactualPos >= 555) && (pageXactualPos <= 665)) && ((pageYactualPos >= 335) && (pageYactualPos <= 550))){
					if((flame.className != 'not-move') && (document.getElementById('turnOn').className == 'Off')){
						flame.className = 'not-move';
						$('#candle-light-content').animate(
							{left:"440px",top:"100px"},400
						);
						/********** select the languaje es = esp / en = english ********/
						setTimeout(which_player('es'),400);
					}
				}    
			}
			
			if(window.innerHeight < window.innerWidth){
			// code for Landscape orientation   
				if (((pageXactualPos >= 865) && (pageXactualPos <= 975)) && ((pageYactualPos >= 165) && (pageYactualPos <= 480))){
					if((flame.className != 'not-move') && (document.getElementById('turnOn').className == 'Off')){
						flame.className = 'not-move';
						$('#candle-light-content').animate(
							{left:"750px",top:"30px"},400
						);
						/********** select the languaje es = esp / en = english ********/
						setTimeout(which_player('es'),400);
					}
				}    
			}
						
			
			
        }, false);
		
		/* don't turn off */
		document.addEventListener('touchend', function(e) {
			
			if(window.innerHeight > window.innerWidth){
			// code for Portrait orientation   
				if ((flame.style.display != 'none') && (flame.offsetLeft != 440 && flame.offsetTop != 100)){
					flame.className = 'moving';
				}
			}
			
			if(window.innerHeight < window.innerWidth){
			// code for Landscape orientation   
				if ((flame.style.display != 'none') && (flame.offsetLeft != 750 && flame.offsetTop != 30)){
					flame.className = 'moving';
				}
			}

        }, false);
		
    });
	


function subVideo1_es(){
	var text = document.getElementById('text-content');
	text.innerHTML='';
	setTimeout(function(){
		text.innerHTML='En el nombre del Padre, y del Hijo, y del Esp&iacute;ritu Santo. Am&eacute;n.';
		setTimeout(function(){
			text.innerHTML='&Aacute;ngel de Dios, que eres mi custodio,';
			setTimeout(function(){
				text.innerHTML='pues la bondad divina<br/>me ha encomendado a ti,';
				setTimeout(function(){
					text.innerHTML='ilum&iacute;name, dir&iacute;geme, gu&aacute;rdame. Am&eacute;n.';
					setTimeout(function(){
						text.innerHTML='En el nombre del Padre, y del Hijo, y del Esp&iacute;ritu Santo. Am&eacute;n.';
						setTimeout(function (){
							text.innerHTML='';
						},4000);
					},3000);
				},3000);
			},2000);
		},5000);
	},0);
		
}

function subVideo1_en(){
	var text = document.getElementById('text-content');
	text.innerHTML='';
	setTimeout(function(){
		text.innerHTML='In the name of the Father, <br/>the Son and the Holy Spirit. Amen.';
		setTimeout(function(){
			text.innerHTML="Angel of God my guardian dear.<br/>To whom God's love commits me here.";
			setTimeout(function(){
				text.innerHTML='Ever this day be at my side,<br/>to light and guard, to rule and guide.<br/>Amen.';
				setTimeout(function(){
					text.innerHTML='In the name of the Father,<br/>the Son and the Holy Spirit. Amen.';
					setTimeout(function(){
						text.innerHTML='';
					},6000);
				},6000);
			},6000);
		},4000);
	},0);
		
}


function subVideo2_es(){
	var text = document.getElementById('text-content');
	text.innerHTML='';
	
	setTimeout(function(){
		text.innerHTML='En el nombre del Padre, y del Hijo, y del Esp&iacute;ritu Santo. Am&eacute;n.';
		setTimeout(function(){
			text.innerHTML='Dios te salve, Maria.';
			setTimeout(function(){
				text.innerHTML='Llena eres de gracia:<br/>El Se&ntilde;or es contigo.';
				setTimeout(function(){
					text.innerHTML='Bendita t&uacute; eres entre todas las mujeres.';
					setTimeout(function(){
						text.innerHTML='Y bendito es el fruto de tu vientre:<br/>Jes&uacute;s.';
						setTimeout(function(){
							text.innerHTML='Santa Mar&iacute;a, Madre de Dios,';
							setTimeout(function(){
								text.innerHTML='ruega por nosotros pecadores,<br/>ahora y en la hora de nuestra muerte.';
								setTimeout(function(){
									text.innerHTML='En el nombre del Padre, y del Hijo, y del Esp&iacute;ritu Santo.  Am&eacute;n.';
									setTimeout(function (){
											text.innerHTML='';
										},4000);
								},4000);
							},2000);
						},2000);
					},2900);
				},4000);
			},2000);
		},4000);
	},0);
}




function subVideo2_en(){
	var text = document.getElementById('text-content');
	text.innerHTML='';
	setTimeout(function(){
		
		text.innerHTML='In the name of the Father,<br/>the Son and the Holy Spirit. Amen';
		
		setTimeout(function(){
			text.innerHTML='Hail Mary, full of grace.<br/>The Lord is with thee.';
			setTimeout(function(){
				text.innerHTML='Blessed art thou amongst women,<br/>and blessed is the fruit of thy womb, Jesus.';
				setTimeout(function(){
					text.innerHTML='Holy Mary, Mother of God,<br/>pray for us sinners,';
					setTimeout(function(){
						text.innerHTML='now and at the hour of our death. Amen.';
						setTimeout(function(){
							text.innerHTML='In the name of the Father,<br/>the Son and the Holy Spirit. Amen';
							setTimeout(function(){
								text.innerHTML='';
							},4500);
						},3000);
					},3500);
				},4500);
			},4000);
		},4500);
	},0);
}

function subVideo3_es(){
	var text = document.getElementById('text-content');
	text.innerHTML='';
	
	setTimeout(function(){
		text.innerHTML='En el nombre del Padre, y del Hijo, y del Esp&iacute;ritu Santo. Am&eacute;n.';
		setTimeout(function(){
			text.innerHTML='Padre nuestro,<br/>que est&aacute;s en los cielo.';
			setTimeout(function(){
				text.innerHTML='Santificado sea tu nombre.<br/>Venga tu reino.';
				setTimeout(function(){
						text.innerHTML='H&aacute;gase tu voluntad,<br/>en la tierra como en el cielo.';
						setTimeout(function(){
							text.innerHTML='Danos hoy nuestro pan de cada d&iacute;a.';
							setTimeout(function(){
								text.innerHTML='Perdona nuestras ofensas,<br/>como tambi&eacute;n nosotros perdonamos a los que nos ofenden.';
								setTimeout(function(){
									text.innerHTML='No nos dejes caer en tentaci&oacute;n<br/>y l&iacute;branos del mal.<br/>Am&eacute;n.';
									setTimeout(function(){
										text.innerHTML='En el nombre del Padre, y del Hijo, y del Esp&iacute;ritu Santo. Am&eacute;n.';
										setTimeout(function (){
											text.innerHTML='';
										},4000);
									},4500);
								},4000);
							},2500);
						},3500);
				},3000);
			},3000);
		},4000);
	},0);
}


function subVideo3_en(){
	var text = document.getElementById('text-content');
	text.innerHTML='';
	setTimeout(function(){
		text.innerHTML='In the name of the Father,<br/>the Son and the Holy Spirit. Amen.';
		setTimeout(function(){
			text.innerHTML='Our Father who art in heaven,<br/>hallowed be thy name.';
			setTimeout(function(){
				text.innerHTML='Thy kingdom come, thy will be done,<br/>on earth as it is in heaven.';
				setTimeout(function(){
					text.innerHTML=' Give us this day our daily bread,';
					setTimeout(function(){
						text.innerHTML=' and forgive us our trespasses<br/>as we forgive those who trespass against us.';
						setTimeout(function(){
							text.innerHTML='And lead us not into temptation,<br/>but deliver us from evil. Amen.';
							setTimeout(function (){
								text.innerHTML='In the name of the Father,<br/>the Son and the Holy Spirit. Amen.';
								setTimeout(function(){
									text.innerHTML='';
								},4500);
							},5500);
						},4500);
					},3000);
				},5000);
			},5000);
		},4000);
	},0);
		
}
